package com.kingtsoft.study.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.kingtsoft.study.common.PageUtil;
import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.model.PageRequest;
import com.kingtsoft.study.model.StudyStudent;
import com.kingtsoft.study.model.StudyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.kingtsoft.study.common.ContextHolder;
import com.kingtsoft.study.common.JsonResult;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.model.StudyHomework;
import com.kingtsoft.study.service.StudyHomeworkService;
/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2017-03-30 下午2:12
 * Company: wondersgroup.com
 *
 * @author jerry.pan(潘健雷)
 * @version 1.0
 */
@Controller
@RequestMapping("study/homework")
public class StudyHomeworkController {

    @Autowired
    StudyHomeworkService homeworkService;

    /**
     * 查看老师布置的作业的列表
     *
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:07
     */
    @RequestMapping(value = "/page/list")
    public ModelAndView pageList(StudyHomework studyHomework) {
        ModelAndView mav = new ModelAndView("/homework/homework_list");
        mav.addObject("user",ContextHolder.getSessionUser());
        mav.addObject("student",ContextHolder.getSessionStudent());
        mav.addObject("teacher",ContextHolder.getSessionTeacher());
        mav.addObject("studyHomework",studyHomework);
        return mav;
    }

    /**
     * 获取老师布置的作业的列表
     *
     * @param studyHomework
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:00
     */
    @RequestMapping("/listPage")
    @ResponseBody
    public JsonResult listPage(PageRequest pageRequest, StudyHomework studyHomework) {
        PageInfo page = PageUtil.getPage(pageRequest);
        PageInfo<StudyHomework> result = homeworkService.listPage(page, studyHomework);
        JsonResult jsonResult = new JsonResult(DescribableEnum.SUCCESS, result);
        return jsonResult;
    }

    /**
     * 获取老师布置的作业的列表
     *
     * @param studyHomework
     * @return
     * @author jerry.pan
     * @date 2017-03-30 14:54:00
     */
    @RequestMapping("/listAll")
    @ResponseBody
    public JsonResult listAll(StudyHomework studyHomework) {
        List<StudyHomework> list = homeworkService.listAll(studyHomework);
        JsonResult jsonResult = new JsonResult(DescribableEnum.SUCCESS, list);
        return jsonResult;
    }

    /**
     *
     * @Title: add
     * @Description: 打开homeword保存页面
     * @author: shenlx
     * @param request
     * @return
     * @return: ModelAndView
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("homework/homework_add");
        return mv;
    }

    /**
     *
     * @Title: doAdd
     * @Description: homework保存
     * @author: shenlx
     * @param studyHomework
     * @return
     * @return: JsonResult
     */
    @RequestMapping("/doAdd")
    @ResponseBody
    public JsonResult doAdd(StudyHomework studyHomework) {

    	homeworkService.addHomework(studyHomework);

        return new JsonResult(DescribableEnum.SUCCESS);
    }


    /**
     *
     * @Title: findByTeacher
     * @Description: 查看登录的当前老师布置的作业的列表
     * @author: shenlx
     * @return
     * @return: ModelAndView
     */
    @RequestMapping(value = "/page/findByTeacher")
    public ModelAndView findByTeacher() {
        ModelAndView mav = new ModelAndView("/homework/homework_teacher");
        return mav;
    }

    /**
     *
     * @Title: findPageByTid
     * @Description: 通过当前登录老师Id获取homework分页
     * @author: shenlx
     * @param pageRequest
     * @param studyHomework
     * @return
     * @return: JsonResult
     */
    @RequestMapping("/findPageByTid")
    @ResponseBody
    public JsonResult findPageByTid(PageRequest pageRequest, StudyHomework studyHomework) {
    	PageInfo<StudyHomework> page = PageUtil.getPage(pageRequest);
    	studyHomework.setTeacherId(ContextHolder.getSessionTeacher().getId());
    	page = homeworkService.listPage(page, studyHomework);
        return new JsonResult(DescribableEnum.SUCCESS,page);
    }


    /**
     *
     * @Title: initBinder
     * @Description: 初始化时间格式，前端String转Date类型时用
     * @author: shenlx
     * @param binder
     * @return: void
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
